home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Shared / MM / Scripts / setText.js < prev   
Encoding:
Text File  |  1999-12-01  |  3.2 KB  |  78 lines

  1. // Copyright 1999 Macromedia, Inc. All rights reserved.
  2.  
  3.  
  4. //**************** GENERIC FUNCTIONS ****************
  5.  
  6. function escExprStr(theStr,isHTML){
  7.   var isMac = (navigator.platform != "Win32");
  8.   var newline = (isMac)? "\x0D" : "\x0D\x0A";
  9.   var i, theChar="", inStr=true, braceCtr=0, escStr="";
  10.   for (i=0; i<theStr.length; i++) {
  11.     theChar = ""+theStr.charAt(i); //get char, convert to string
  12.     if ((theChar == "{" || theChar == "}") && (i==0 || theStr.charAt(i-1) != "\\")) { //if switching modes
  13.       inStr = (theChar == "}"); //set inStr to false when entering {region}
  14.       braceCtr += (inStr)? -1 : 1;
  15.       if (braceCtr<0 || braceCtr>1) break; //bad braces, bail out
  16.       theChar = (inStr)? ")+'" : "'+(";
  17.     } else if (inStr) { //only if in a string
  18.       if (theChar == "\\" && i<theStr.length-1 && (theStr.charAt(i+1)=="{" || theStr.charAt(i+1)=="}"))
  19.         theChar = ""; //skip escapes for {} (show literal brace)
  20.       if (isHTML) {
  21.         if (theChar != " ") theChar = dw.doURLEncoding(theChar); //URL-encode the char if needed
  22.       } else if (theStr.substring(i,theStr.length).indexOf(newline) == 0) { //find \r\n or \n (mac)
  23.         theChar = "\\"+"r"; //make \r for JS
  24.         i += newline.length - 1;
  25.       } else { //normal, non-html
  26.         theChar = theChar.replace(/\\/,"\\\\"); //escape \
  27.       }
  28.       //escape quotes, requires 2 calls due to RegExp bug
  29.       theChar = theChar.replace(/\'/,"\\'");  //escape '
  30.       theChar = theChar.replace(/\"/,"\\\""); //escape "
  31.     } else { //in expression
  32.       theChar = theChar.replace(/[\x0A\x0D]/g,""); //remove newlines
  33.     }
  34.     escStr += theChar; //add to new string
  35.   }
  36.   if (braceCtr!=0) escStr = null; //bad braces, return error code null
  37.   else escStr = escStr.replace(/\'\+\(\)\+\'/g,""); //remove any empty expressions
  38.   return escStr;
  39. }
  40.  
  41.  
  42. function unescExprStr(theStr,isHTML){
  43.   var isMac = (navigator.platform != "Win32");
  44.   var newline = (isMac)? "\x0D" : "\x0D\x0A";
  45.   if (!theStr) theStr="";
  46.   if (isHTML) { //re-escape all literal braces { or }
  47.     theStr = theStr.replace(/\%7B/g,"\\{");
  48.     theStr = theStr.replace(/\%7D/g,"\\}");
  49.   } else {
  50.     theStr = theStr.replace(/\{/g,"\\{");
  51.     theStr = theStr.replace(/\}/g,"\\}");
  52.   }
  53.   theStr = theStr.replace(/['"]\s*\+\s*\(/g,"{"); //replace any '+( or "+( with {
  54.   theStr = theStr.replace(/\)\s*\+\s*['"]/g,"}"); //replace any )+' or )+" with }
  55.   theStr = theStr.replace(/\\(['"\x5C])/g,"$1");  //unescape all ',",\
  56.   if (isHTML) {
  57.     theStr = unescape(theStr);
  58.   } else {
  59.     theStr = theStr.replace(/\\r/g,newline); //convert \r back to \r\n
  60.   }
  61.   return (theStr);
  62. }
  63.  
  64.  
  65. function extractExprStr(behFnCallStr){
  66.   var i, theStr, lastPos, argArray;
  67.  
  68.   behFnCallStr = behFnCallStr.replace(/^[^\(]+\(/, ""); //remove outer fn call stuff "fn("
  69.   behFnCallStr = behFnCallStr.replace(/\);?$/, "");        //remove outer fn call stuff ")"
  70.   argArray = dreamweaver.getTokens(behFnCallStr,",");
  71.   for (i=0; i<argArray.length; i++) {
  72.     argArray[i] = argArray[i].replace(/\\(['"])/g,"$1"); //unescape quotes
  73.     argArray[i] = argArray[i].replace(/^\s*['"]/,""); //remove leading quote
  74.     argArray[i] = argArray[i].replace(/['"]\s*$/,""); //remove trailing quote
  75.   }
  76.   return argArray
  77. }
  78.